using System;
using System.Net.Mail;

namespace Lombiq.HelpfulLibraries.Common.Validation;

/// <summary>
/// 验证电子邮件地址的助手。
/// </summary>
public static class EmailValidationHelpers
{
    /// <summary>
    /// 确定提供的字符串是否为有效的电子邮件地址.
    /// </summary>
    /// <returns>
    /// 如果给定字符串是有效的电子邮件地址，则返回 <see langword="true"/>，否则返回 <see langword="false"/>.
    /// </returns>
    public static bool IsValidEmailAddress(string email)
    {
        try
        {
            _ = new MailAddress(email);

            return true;
        }
        catch (FormatException)
        {
            return false;
        }
    }

    /// <summary>
    /// 确定提供的字符串是否为有效的逗号分隔电子邮件地址列表.
    /// </summary>
    /// <returns>
    /// 如果给定字符串是有效的逗号分隔电子邮件地址列表，则返回 <see langword="true"/>，否则返回 <see langword="false"/>.
    /// </returns>
    public static bool IsValidCommaSeparatedEmailAddressList(string emailAddresses) =>
        emailAddresses.SplitByCommas().TrueForAll(IsValidEmailAddress);
}
